package com.dehui.traders.ui.market.broader;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.bean.IndexInfo;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.List;

import me.goldze.mvvmhabit.utils.Utils;

/**
 * Created by hqq on 18.4.25.
 */

public class TheBroaderAdapter extends TableAdapter<IndexInfo> {

    private static final int Index_TheNew = 0;
    private static final int Index_ZhangDieFu = 1;
    private static final int Index_ZhangDieE = 2;
    private static final int Index_KaiPanJia = 3;
    private static final int Index_ZuiGaoJia = 4;
    private static final int Index_ZuiDiJia = 5;
    private static final int Index_ZuoRiShouPanJia = 6;
    private static final int Index_ChengJia0Liang = 7;
    private static final int Index_ChengJiaoE = 8;
    private Fragment fragment;

    public TheBroaderAdapter(Context context, List mDatas, Fragment fragment) {
        super(context, mDatas);
        this.fragment = fragment;
    }

    @Override
    public void setFragment(MyHScrollView scrollview) {
        scrollview.setFragemnt(fragment);
    }

    @Override
    public void setItemText(int position, TextView tv,IndexInfo item) {
        double cha = item.close - item.open;
        switch (position){
            case Index_TheNew:
                tv.setText(Utils.doubleToString(item.close));
                if (cha < 0){
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.green));
                }else if (cha > 0){
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                }else{
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.white));
                }
                break;
            case Index_ZhangDieFu:
                tv.setText(item.change);
                tv.setTextColor(Color.RED);
                if (cha < 0){
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.green));
                }else if (cha > 0){
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                }else{
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.white));
                }
                break;
            case Index_ZhangDieE:
                tv.setText(Utils.doubleToString(item.high - item.low));
                if (cha < 0){
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.green));
                }else if (cha > 0){
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                }else{
                    tv.setTextColor(ContextCompat.getColor(mContext,R.color.white));
                }
                break;
            case Index_KaiPanJia:
                tv.setText(Utils.doubleToString(item.open));
                tv.setTextColor(ContextCompat.getColor(mContext,R.color.green));
                break;
            case Index_ZuiGaoJia:
                tv.setText(Utils.doubleToString(item.high));
                tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                break;
            case Index_ZuiDiJia:
                tv.setText(Utils.doubleToString(item.low));
                tv.setTextColor(ContextCompat.getColor(mContext,R.color.green));
                break;
            case Index_ZuoRiShouPanJia:
                tv.setText(Utils.doubleToString(item.preclose));
                tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                break;
            case Index_ChengJia0Liang:
                Double val = Double.valueOf(item.volume);
                tv.setText(Utils.doubleToString(val / 10000) + "万");
                tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                break;
            case Index_ChengJiaoE:
                tv.setText(Utils.doubleToString(item.amount / 10000));
                tv.setTextColor(ContextCompat.getColor(mContext,R.color.red));
                break;
        }

    }

    @Override
    public SpannableString setTv0(IndexInfo item) {
        String title = item.name + "\n" + item.code;
        SpannableString styledText = new SpannableString(title);
        styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_title_0), 0, item.name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_title_1), item.name.length(), title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return styledText;
    }

    @Override
    public int getL() {
        return 9;
    }


}
